package com.doctor.ui.consulting.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.doctor.base.BaseFragment;
import com.doctor.constants.CodeHelper;
import com.doctor.ui.R;
import com.doctor.ui.consulting.model.SickenInfoEntity;
import com.doctor.ui.consulting.presenter.ConsultationPresenter;
import com.doctor.utils.callback.DataChangedListener;
import com.doctor.utils.logutils.LogUtils;
import com.doctor.utils.sys.ToastUtils;

/**
 * 会诊单界面
 * Created by chemin on 2017/3/12 16:11.
 */

public class ConsultationListFrag extends BaseFragment implements View.OnClickListener {

    private RadioGroup mRbGroup;
    private RadioButton mRbMan;
    private RadioButton mRbWomen;
    private ConsultationPresenter mConsultationListPresenter;
    private TextView mTvConsultationMenu;
    private TextView mTvGetRecordMenu;
    private DataChangedListener mListener;
    private EditText mEtUserName;
    private EditText mEtUserOld;
    private EditText mEtUserAddr;
    private EditText mEtUserInformation;
    private EditText mEtSickenInfo;
    private EditText mEtCheckInfo;
    private TextView mUpload;
    private TextView mTakePhotoBtn;
    private EditText mEtRequirment;
    private EditText mEtSignature;
    private TextView mNextBtn;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_consultaion_list_layout;
    }

    @Override
    protected void initView(View view) {
        mRbGroup = (RadioGroup) view.findViewById(R.id.consultation_rb_sex);
        mRbMan=(RadioButton)view.findViewById(R.id.consultaion_rb_man);
        mRbWomen=(RadioButton)view.findViewById(R.id.consultaion_rb_woman);
        mTvConsultationMenu=(TextView)view.findViewById(R.id.tv_consultation_menu);//会诊列表
        mTvGetRecordMenu=(TextView)view.findViewById(R.id.tv_get_record_menu);//获取以前的病历
        mEtUserName=(EditText)view.findViewById(R.id.et_user_name);//用户名
        mEtUserOld=(EditText)view.findViewById(R.id.et_user_old);//用户年龄
        mEtUserAddr=(EditText)view.findViewById(R.id.et_user_addr);//
        mEtUserInformation=(EditText)view.findViewById(R.id.et_user_infomation);//
        mEtSickenInfo=(EditText)view.findViewById(R.id.et_user_sicken_infomation);//患者疾病情况
        mEtCheckInfo=(EditText)view.findViewById(R.id.et_user_check_infomation);//辅助检查
        mEtRequirment=(EditText)view.findViewById(R.id.et_consultation_requirment);//
        mEtSignature=(EditText)view.findViewById(R.id.et_signature);//医生签名
        mUpload=(TextView)view.findViewById(R.id.tv_consultation_upload);
        mTakePhotoBtn=(TextView)view.findViewById(R.id.tv_consultation_take_photo);
        mNextBtn=(TextView)view.findViewById(R.id.tv_consultation_next);
    }

    /**
     * 设置替换fragment的listener
     * @param listener
     */
    public void setReplaceFragmentListener(DataChangedListener listener){
        this.mListener=listener;
    }

    @Override
    protected void initData() {
        mConsultationListPresenter = new ConsultationPresenter(consultationView);

    }

    @Override
    protected void addListener() {
        mTvConsultationMenu.setOnClickListener(this);
        mTvGetRecordMenu.setOnClickListener(this);
        mUpload.setOnClickListener(this);
        mTakePhotoBtn.setOnClickListener(this);
        mNextBtn.setOnClickListener(this);

    }

    @Override
    protected void setData() {
        mRbMan.setChecked(true);//默认男

    }

    @Override
    public void onClick(View view) {
        if (view!=null){
            switch (view.getId()){
                case R.id.tv_consultation_menu:
                    //开始加载到会诊列表界面fragment,使用接口回调
                    if (mListener!=null){
                        mListener.dataChanged(CodeHelper.CONSULTATION_MENU,null);
                    }
                    break;
                case R.id.tv_get_record_menu:
                    mConsultationListPresenter.getRecordData();
                    break;
                case R.id.tv_consultation_upload:
                    //开始上传图片

                    break;
                case R.id.tv_consultation_take_photo:
                    //开始拍照然后上传

                    break;
                case R.id.tv_consultation_next://开始进入下一步
                    consultationListNext();
                    break;
                default:
                    break;
            }
        }

    }

    private void consultationListNext() {

        String userName = mEtUserName.getText().toString().trim();
        String userOld = mEtUserOld.getText().toString().trim();
        String userAdd = mEtUserAddr.getText().toString().trim();
        String userInfomation = mEtUserInformation.getText().toString().trim();
        String userSickenInfo = mEtSickenInfo.getText().toString().trim();
        String userCheckInfo = mEtCheckInfo.getText().toString().trim();
        String requirment = mEtRequirment.getText().toString().trim();
        String signature = mEtSignature.getText().toString().trim();
        if (TextUtils.isEmpty(userName)||TextUtils.isEmpty(userOld)||TextUtils.isEmpty(userAdd)
                ||TextUtils.isEmpty(userInfomation)||TextUtils.isEmpty(userSickenInfo)
                ||TextUtils.isEmpty(userCheckInfo)||TextUtils.isEmpty(requirment)||TextUtils.isEmpty(signature)
                ){
            ToastUtils.showLongToast(getActivity(),getString(R.string.user_info_not_empty));
            LogUtils.e("信息还未填完整");
            return;
        }else {
            SickenInfoEntity sickenInfoEntity=new SickenInfoEntity();
            sickenInfoEntity.setUserName(userName);
            sickenInfoEntity.setUserOld(userOld);
            sickenInfoEntity.setUserAdd(userAdd);
            sickenInfoEntity.setUserInfomation(userInfomation);
            sickenInfoEntity.setUserSickenInfo(userSickenInfo);
            sickenInfoEntity.setUserCheckInfo(userCheckInfo);
            sickenInfoEntity.setRequirment(requirment);
            sickenInfoEntity.setSignature(signature);
            if (mListener!=null){
                mListener.dataChanged(CodeHelper.PROFESSOR_LIST_FRAGMENT,sickenInfoEntity);
            }
        }
    }

    //-------view的实现
    IConsultationView consultationView=new IConsultationView() {
        @Override
        public void showToast(String message) {
            ToastUtils.showLongToast(getActivity(),message);
        }

        @Override
        public Context getContext() {
            return getActivity();
        }

        @Override
        public void updateRecordeData(Object obj) {
            //开始更新诊单信息
            if (obj!=null){

            }else {
                showToast(getString(R.string.not_record));
            }

        }
    };

}
